<!DOCTYPE html>
<html lang="zh-CN">
    <head>
    <meta charset="utf-8">

    

    <!-- 渲染优化 -->
    <meta name="renderer" content="webkit">
    <meta name="force-rendering" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="HandheldFriendly" content="True" >
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <!--icon-->

    
        <link rel="shortcut icon" href="https://s1.imgpp.com/2022/01/28/owl.png">
    
    
    
    
    


    <!-- meta -->


<title>webpack的使用 | 猫头鹰的BLOG</title>


    <meta name="keywords" content="webpack, 猫头鹰的博客">




    <!-- OpenGraph -->
 
    <meta name="description" content="准备及配置 安装webpack及脚手架  npm install webpack@5.42.1 webpack-cli@4.7.2 -D   项目根目录下创建webpack.config.js  module.exports &#x3D; &amp;#123;     mode:&amp;#x27;development&amp;#x27;	&#x2F;&#x2F;production或development &amp;#125;   在package.js">
<meta property="og:type" content="article">
<meta property="og:title" content="webpack的使用">
<meta property="og:url" content="http://example.com/2022/03/18/webpack%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="猫头鹰的BLOG">
<meta property="og:description" content="准备及配置 安装webpack及脚手架  npm install webpack@5.42.1 webpack-cli@4.7.2 -D   项目根目录下创建webpack.config.js  module.exports &#x3D; &amp;#123;     mode:&amp;#x27;development&amp;#x27;	&#x2F;&#x2F;production或development &amp;#125;   在package.js">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://images.pexels.com/photos/5319706/pexels-photo-5319706.jpeg?cs=srgb&dl=pexels-roman-odintsov-5319706.jpg&fm=jpg">
<meta property="article:published_time" content="2022-03-18T02:00:20.000Z">
<meta property="article:modified_time" content="2022-03-26T04:54:50.324Z">
<meta property="article:author" content="张程">
<meta property="article:tag" content="webpack">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://images.pexels.com/photos/5319706/pexels-photo-5319706.jpeg?cs=srgb&dl=pexels-roman-odintsov-5319706.jpg&fm=jpg">


    
<link rel="stylesheet" href="/css/style/main.css">
 

    
    
    
        <link rel="stylesheet" id="hl-default-theme" href="https://cdn.jsdelivr.net/npm/highlight.js@10.1.2/styles/arduino-light.min.css" media="none" >
        
            <link rel="stylesheet" id="hl-dark-theme" href="https://cdn.jsdelivr.net/npm/highlight.js@10.1.2/styles/obsidian.min.css" media="none">
        
    

    

    
    
<link rel="stylesheet" href="/css/style/dark.css">

    
<script src="/js/darkmode.js"></script>



     
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
 

    <!-- custom head -->

<meta name="generator" content="Hexo 6.0.0"></head>

    <body>
        <div id="app">
            <header class="header">
    <div class="header__left">
        <a href="/" class="button">
            <span class="logo__text">猫头鹰</span>
        </a>
    </div>
    <div class="header__right">
        
            <div class="navbar__menus">
                
                    <a href="/" class="navbar-menu button">首页</a>
                
                    <a href="/tags/" class="navbar-menu button">标签</a>
                
                    <a href="/categories/" class="navbar-menu button">分类</a>
                
                    <a href="/archives/" class="navbar-menu button">归档</a>
                
            </div>
        
        
        
    <a href="/search/" id="btn-search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" width="24" height="24" fill="currentColor" stroke="currentColor" stroke-width="32"><path d="M192 448c0-141.152 114.848-256 256-256s256 114.848 256 256-114.848 256-256 256-256-114.848-256-256z m710.624 409.376l-206.88-206.88A318.784 318.784 0 0 0 768 448c0-176.736-143.264-320-320-320S128 271.264 128 448s143.264 320 320 320a318.784 318.784 0 0 0 202.496-72.256l206.88 206.88 45.248-45.248z"></path></svg>
    </a>


        
        
    <a href="javaScript:void(0);" id="btn-toggle-dark">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
    </a>


        
            <a class="dropdown-icon button" id="btn-dropdown" tabindex="0"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width='24' height='24' fill="none" stroke="currentColor" stroke-width="0.7" stroke-linecap="round" stroke-linejoin="round"><path fill="currentColor" d="M3.314,4.8h13.372c0.41,0,0.743-0.333,0.743-0.743c0-0.41-0.333-0.743-0.743-0.743H3.314c-0.41,0-0.743,0.333-0.743,0.743C2.571,4.467,2.904,4.8,3.314,4.8z M16.686,15.2H3.314c-0.41,0-0.743,0.333-0.743,0.743s0.333,0.743,0.743,0.743h13.372c0.41,0,0.743-0.333,0.743-0.743S17.096,15.2,16.686,15.2z M16.686,9.257H3.314c-0.41,0-0.743,0.333-0.743,0.743s0.333,0.743,0.743,0.743h13.372c0.41,0,0.743-0.333,0.743-0.743S17.096,9.257,16.686,9.257z"></path></svg></a>
            <div class="dropdown-menus" id="dropdown-menus">
                
                    <a href="/" class="dropdown-menu button">首页</a>
                
                    <a href="/tags/" class="dropdown-menu button">标签</a>
                
                    <a href="/categories/" class="dropdown-menu button">分类</a>
                
                    <a href="/archives/" class="dropdown-menu button">归档</a>
                
            </div>
        
    </div>
</header>


            <main class="main">
    

<div class="post-title">
    <h1 class="post-title__text">
        webpack的使用
    </h1>
    <div class="post-title__meta">
        <a href="/archives/2022/03/" class="post-meta__date button">2022-03-18</a>
        
    <span class="separate-dot"></span><a href="/categories/webpack/" class="button">webpack</a>

 
        
    
     
    <span id="busuanzi_container_page_pv" hidden>
        <span class="separate-dot"></span>
        <span></span>
        <span id="busuanzi_value_page_pv"></span>
        <span>次观看</span>
    </span>



 

 
    </div>
</div>



<article class="post content-card">
    <div class="post__header">
    
        <div class="post-thumbnail lazy" data-bg="https://images.pexels.com/photos/5319706/pexels-photo-5319706.jpeg?cs=srgb&amp;dl=pexels-roman-odintsov-5319706.jpg&amp;fm=jpg"></div>
    
</div>
    <div class="post__content">
        <h2 id="准备及配置"><a href="#准备及配置" class="headerlink" title="准备及配置"></a>准备及配置</h2><blockquote>
<p>安装webpack及脚手架</p>
</blockquote>
<pre><code class="hljs shell">npm install webpack@5.42.1 webpack-cli@4.7.2 -D</code></pre>

<blockquote>
<p>项目根目录下创建webpack.config.js</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-built_in">module</span>.exports = &#123;
    <span class="hljs-attr">mode</span>:<span class="hljs-string">&#x27;development&#x27;</span>	<span class="hljs-comment">//production或development</span>
&#125;</code></pre>

<blockquote>
<p>在package.json中添加一个配置项，”type”:”module”要去掉</p>
</blockquote>
<pre><code class="hljs json"><span class="hljs-string">&quot;scripts&quot;</span>: &#123;
    <span class="hljs-attr">&quot;dev&quot;</span>:<span class="hljs-string">&quot;webpack&quot;</span>	<span class="hljs-comment">//可以npm run 命令</span>
&#125;,</code></pre>

<h2 id="指定webpack的entry和output"><a href="#指定webpack的entry和output" class="headerlink" title="指定webpack的entry和output"></a>指定webpack的entry和output</h2><h4 id="默认entry和output"><a href="#默认entry和output" class="headerlink" title="默认entry和output"></a>默认entry和output</h4><blockquote>
<p>在webpack4.x和5.x的版本中，约定：</p>
</blockquote>
<ol>
<li>默认打包入口文件：src -&gt;index.js</li>
<li>默认输出文件路径：dist -&gt;main.js</li>
</ol>
<h4 id="自定义entry和output"><a href="#自定义entry和output" class="headerlink" title="自定义entry和output"></a>自定义entry和output</h4><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;path&quot;</span>)	<span class="hljs-comment">//导入node.js中专门操作路径的模块</span>

<span class="hljs-built_in">module</span>.exports = &#123;
    <span class="hljs-attr">mode</span>: <span class="hljs-string">&#x27;production&#x27;</span>,
    <span class="hljs-attr">entry</span>: path.join(__dirname, <span class="hljs-string">&#x27;./src/index.js&#x27;</span>),  <span class="hljs-comment">//打包文件的路径</span>
    <span class="hljs-attr">output</span>: &#123;
        <span class="hljs-attr">path</span>: path.join(__dirname, <span class="hljs-string">&#x27;./dist&#x27;</span>),   <span class="hljs-comment">//输出文件的存放路径</span>
        <span class="hljs-attr">filename</span>:<span class="hljs-string">&#x27;bundle.js&#x27;</span>
    &#125;
&#125;</code></pre>

<h2 id="webpack-dev-server插件"><a href="#webpack-dev-server插件" class="headerlink" title="webpack-dev-server插件"></a>webpack-dev-server插件</h2><h4 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h4><blockquote>
<p>webpack会自动进行项目打包和构建</p>
</blockquote>
<pre><code class="hljs shell">npm install webpack-dev-server@3.11.2 -D</code></pre>

<h4 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h4><blockquote>
<p>修改package.json的scripts配置项</p>
</blockquote>
<pre><code class="hljs json"><span class="hljs-string">&quot;scripts&quot;</span>: &#123;
	<span class="hljs-attr">&quot;dev&quot;</span>: <span class="hljs-string">&quot;webpack serve&quot;</span>
&#125;,</code></pre>
<blockquote>
<p>运行项目时只要输入一次npm run dev ,webpack就会自动打包</p>
</blockquote>
<p><img src="https://imgpp.com/s1/2022/03/18/image.png" class="lazy" data-srcset="https://imgpp.com/s1/2022/03/18/image.png" srcset="" alt="图片"></p>
<h4 id="webpack-dev-serve的工作原理"><a href="#webpack-dev-serve的工作原理" class="headerlink" title="webpack-dev-serve的工作原理"></a>webpack-dev-serve的工作原理</h4><blockquote>
<p>webpack-dev-serve会自动打包输出在/bundle.js，所有index.html引入应修改为<br>其打包在内存中，而不是磁盘，节省空间，速度快</p>
</blockquote>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;/bundle.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></code></pre>

<h2 id="html-webpack-plugin插件"><a href="#html-webpack-plugin插件" class="headerlink" title="html-webpack-plugin插件"></a>html-webpack-plugin插件</h2><h4 id="安装-1"><a href="#安装-1" class="headerlink" title="安装"></a>安装</h4><pre><code class="hljs shell">npm install html-webpack-plugin@5.3.2 -D</code></pre>

<h4 id="配置-1"><a href="#配置-1" class="headerlink" title="配置"></a>配置</h4><blockquote>
<p>在webpack.config.js中进行配置</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">const</span> HtmlPlugin=<span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;html-webpack-plugin&quot;</span>) <span class="hljs-comment">//1.导入html插件，得到一个构造函数</span>

<span class="hljs-comment">// 2.创建一个html插件的实例化对象</span>
<span class="hljs-keyword">const</span> htmlPlugin = <span class="hljs-keyword">new</span> HtmlPlugin(&#123;
    <span class="hljs-attr">template</span>: <span class="hljs-string">&#x27;./src/index.html&#x27;</span>,   <span class="hljs-comment">//原文件位置</span>
    <span class="hljs-attr">filename</span>:<span class="hljs-string">&#x27;./index.html&#x27;</span> <span class="hljs-comment">//指定生成的文件存放路径</span>
&#125;)

<span class="hljs-built_in">module</span>.exports = &#123;
	<span class="hljs-comment">//添加plugins配置项</span>
    <span class="hljs-attr">plugins</span>:[htmlPlugin]    <span class="hljs-comment">//3.通过plugins节点，使HtmlPlugin插件生效</span>
&#125;</code></pre>

<h4 id="特性"><a href="#特性" class="headerlink" title="特性"></a>特性</h4><blockquote>
<p>将指定页面复制一份放入根目录，也放到了内存中<br>html插件生成的index.html页面，自动注入了打包的bundle.js文件</p>
</blockquote>
<h4 id="devServer"><a href="#devServer" class="headerlink" title="devServer"></a>devServer</h4><blockquote>
<p>在webpack.config.js中添加devServer配置项</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-built_in">module</span>.exports = &#123;
  <span class="hljs-attr">devServer</span>: &#123;
    <span class="hljs-attr">open</span>: <span class="hljs-literal">true</span>,	<span class="hljs-comment">//每次npm run dev自动打开浏览器</span>
    <span class="hljs-attr">port</span>:<span class="hljs-number">80</span>,	<span class="hljs-comment">//在http协议中,80可以省略</span>
    <span class="hljs-attr">host</span>:<span class="hljs-string">&quot;127.0.0.1&quot;</span>	<span class="hljs-comment">//修改主机号</span>
  &#125;
&#125;</code></pre>

<h2 id="loader"><a href="#loader" class="headerlink" title="loader"></a>loader</h2><blockquote>
<p>在webpack中一切皆模块<br>webpack只能打包.js后缀的文件<br>当webpack打包不了，会找到webpack.config.js中module.rules数组中是否配置了对应的loader加载器<br>多个loader调用顺序：从后往前，当多个loader处理完，会把处理结果给webpack<br>webpack将结果合并到/dist/bundle.js中，最终生成打包好的文件</p>
</blockquote>
<h4 id="安装-2"><a href="#安装-2" class="headerlink" title="安装"></a>安装</h4><ol>
<li><p>打包处理css文件</p>
<pre><code class="hljs shell">npm i style-loader@3.0.0 css-loader@5.2.6 -D</code></pre>
<blockquote>
<p>在webpack.config.js中添加配置项</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-built_in">module</span>.exports = &#123;
  <span class="hljs-attr">module</span>: &#123;
    <span class="hljs-attr">rules</span>: [
      &#123;
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.css$/</span>,	<span class="hljs-comment">//test表示文件类型，\.是转义符：</span>
        use:[<span class="hljs-string">&#x27;style-loader&#x27;</span>,<span class="hljs-string">&#x27;css-loader&#x27;</span>]	
      &#125;
    ]
  &#125;
&#125;</code></pre></li>
<li><p>打包处理less文件</p>
<pre><code class="hljs shell">npm i less-loader@10.0.1 less@4.1.1 -D</code></pre>
<blockquote>
<p>在webpack.config.js中添加配置项</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-built_in">module</span>.exports = &#123;
  <span class="hljs-attr">module</span>: &#123;
    <span class="hljs-attr">rules</span>: [
      &#123;
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.less$/</span>,	<span class="hljs-comment">//test表示文件类型，\.是转义符：</span>
        use:[<span class="hljs-string">&#x27;style-loader&#x27;</span>,<span class="hljs-string">&#x27;css-loader&#x27;</span>,<span class="hljs-string">&quot;less-loader&quot;</span>]	
      &#125;
    ]
  &#125;
&#125;</code></pre></li>
<li><p>打包处理样式表中与url路径相关的文件</p>
<pre><code class="hljs shell">npm i url-loader@4.1.1 file-loader@6.2.0 -D</code></pre>
<blockquote>
<p>在webpack.config.js中添加配置项</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-built_in">module</span>.exports = &#123;
  <span class="hljs-attr">module</span>: &#123;
    <span class="hljs-attr">rules</span>: [
      &#123;
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.jpg|png|gif$/</span>,
        use:<span class="hljs-string">&#x27;url-loader?limit=22229&#x27;</span>	<span class="hljs-comment">//只有小于limit大小的图片，才会转化为base64的图片</span>
      &#125;,
    ]
  &#125;
&#125;</code></pre></li>
<li><p>打包处理js文件的高级语法</p>
<blockquote>
<p>对于webpack都不能打包的js高级语法，需要借助于babel-loader进行打包处理</p>
</blockquote>
<pre><code class="hljs shell">npm i babel-loader@8.2.2 @babel/core@7.14.6 @babel/plugin-proposal-decorators@7.14.5 -D</code></pre>
<blockquote>
<p>在webpack.config.js中添加配置项</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-built_in">module</span>.exports = &#123;
  <span class="hljs-attr">module</span>: &#123;
    <span class="hljs-attr">rules</span>: [
      &#123;
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.js$/</span>,
        use: <span class="hljs-string">&#x27;babel-loader&#x27;</span>,
        <span class="hljs-attr">exclude</span>:<span class="hljs-regexp">/node_modules/</span>  <span class="hljs-comment">//排除项：排除node_modules下的包</span>
      &#125;,
    ]
  &#125;
&#125;</code></pre>
<blockquote>
<p>在根目录下创babel.config.js,进行配置</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-built_in">module</span>.exports = &#123;
    <span class="hljs-comment">// 声明babel可用的插件</span>
    <span class="hljs-comment">// 这是插件的插件</span>
    <span class="hljs-attr">plugins</span>: [
        [<span class="hljs-string">&#x27;@babel/plugin-proposal-decorators&#x27;</span>,&#123;<span class="hljs-attr">legacy</span>:<span class="hljs-literal">true</span>&#125;]
    ]
&#125;</code></pre></li>
</ol>
<h2 id="项目发布"><a href="#项目发布" class="headerlink" title="项目发布"></a>项目发布</h2><h4 id="配置build命令"><a href="#配置build命令" class="headerlink" title="配置build命令"></a>配置build命令</h4><blockquote>
<p>项目发布命令,实质就是将开发版改为生产版<br>在package.json中进行配置</p>
</blockquote>
<pre><code class="hljs json"><span class="hljs-string">&quot;scripts&quot;</span>: &#123;
    <span class="hljs-attr">&quot;dev&quot;</span>: <span class="hljs-string">&quot;webpack serve&quot;</span>, 
    <span class="hljs-attr">&quot;build&quot;</span>:<span class="hljs-string">&quot;webpack --mode production&quot;</span>
  &#125;</code></pre>
<blockquote>
<p>package.json中–mode优先级比webpack.config.js高<br>在开发时，生成的文件在内存中<br>在生产时，生成的文件在物理磁盘上</p>
</blockquote>
<h4 id="优化图片和js的存放路径"><a href="#优化图片和js的存放路径" class="headerlink" title="优化图片和js的存放路径"></a>优化图片和js的存放路径</h4><blockquote>
<p>在webpack.config.js中修改配置项</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-built_in">module</span>.exports = &#123;
  <span class="hljs-attr">output</span>: &#123;
    <span class="hljs-attr">path</span>: path.join(__dirname, <span class="hljs-string">&quot;./dist&quot;</span>), 
    <span class="hljs-attr">filename</span>: <span class="hljs-string">&quot;js/bundle.js&quot;</span>,	<span class="hljs-comment">//js文件的存放路径</span>
  &#125;,
  <span class="hljs-attr">module</span>: &#123; <span class="hljs-comment">//第三方包模块的规则</span>
    <span class="hljs-attr">rules</span>: [
      &#123;
        <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.jpg|png|gif$/</span>,
        use:<span class="hljs-string">&#x27;url-loader?limit=22229&amp;outputPath=images&#x27;</span>	<span class="hljs-comment">//添加参数outputPath:指定图片的存放路径</span>
      &#125;
    ]
  &#125;
&#125;;</code></pre>

<h4 id="清理打包发布dist目录的旧文件"><a href="#清理打包发布dist目录的旧文件" class="headerlink" title="清理打包发布dist目录的旧文件"></a>清理打包发布dist目录的旧文件</h4><blockquote>
<p>安装插件Clean plugin for webpack</p>
</blockquote>
<pre><code class="hljs shell">npm install --save-dev clean-webpack-plugin</code></pre>
<blockquote>
<p>在webpack.json.js中进行配置</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-keyword">const</span> &#123; CleanWebpackPlugin &#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;clean-webpack-plugin&#x27;</span>); <span class="hljs-comment">//右边是一个对象，这里是解构赋值</span>
<span class="hljs-built_in">module</span>.exports = &#123;
    <span class="hljs-attr">plugins</span>: [<span class="hljs-keyword">new</span> CleanWebpackPlugin()],
&#125;;</code></pre>

<h2 id="SourceMap"><a href="#SourceMap" class="headerlink" title="SourceMap"></a>SourceMap</h2><h4 id="配置-2"><a href="#配置-2" class="headerlink" title="配置"></a>配置</h4><blockquote>
<p>由于浏览器控制台的报错的行号和源文件的行号对于不上，所以用到SourceMap<br>SourceMap他是一个信息储存文件，储存着位置信息，也就是压缩混淆后的代码和之前代码所对应的位置信息<br>在webpack.json.js中配置</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-built_in">module</span>.exports = &#123;
    <span class="hljs-attr">devtool</span>: <span class="hljs-string">&quot;eval-source-map&quot;</span>	<span class="hljs-comment">//在开发阶段，建议把devtool的值设置为eval-source-map</span>
&#125;;</code></pre>

<h4 id="发布时关闭"><a href="#发布时关闭" class="headerlink" title="发布时关闭"></a>发布时关闭</h4><blockquote>
<p>不关闭，能通过控制台拿到源代码，不安全</p>
</blockquote>
<h4 id="只定行号不漏源码"><a href="#只定行号不漏源码" class="headerlink" title="只定行号不漏源码"></a>只定行号不漏源码</h4><blockquote>
<p>只需将webpack.json.js中devtool配置项改为nosources-source-map</p>
</blockquote>
<h4 id="定行号漏源码"><a href="#定行号漏源码" class="headerlink" title="定行号漏源码"></a>定行号漏源码</h4><blockquote>
<p>只需将webpack.json.js中devtool配置项改为source-map</p>
</blockquote>
<h4 id="SourceMap的最佳实践"><a href="#SourceMap的最佳实践" class="headerlink" title="SourceMap的最佳实践"></a>SourceMap的最佳实践</h4><ol>
<li>开发时建议devtool配置项改为eval-source-map<blockquote>
<p>能精准定位行号</p>
</blockquote>
</li>
<li>发布时建议devtool配置项改为nosources-source-map<blockquote>
<p>防止源代码泄露</p>
</blockquote>
</li>
</ol>
<h2 id="拓展"><a href="#拓展" class="headerlink" title="拓展"></a>拓展</h2><blockquote>
<p>在导入模块时，一般会遇到从里往外导包<br>建议使用@表示src目录，从外往里找<br>在webpack.json.js中配置</p>
</blockquote>
<pre><code class="hljs javascript"><span class="hljs-built_in">module</span>.exports = &#123;
   <span class="hljs-attr">resolve</span>: &#123;
    <span class="hljs-attr">alias</span>: &#123;
      <span class="hljs-string">&quot;@&quot;</span>:path.join(__dirname,<span class="hljs-string">&#x27;./src/&#x27;</span>)
    &#125;
   &#125;</code></pre>
<blockquote>
<p>用的时候@/</p>
</blockquote>
<h4 id="webpack和less-loader版本冲突"><a href="#webpack和less-loader版本冲突" class="headerlink" title="webpack和less-loader版本冲突"></a>webpack和less-loader版本冲突</h4><blockquote>
<p>解决方法：</p>
</blockquote>
<pre><code class="hljs shell">npm install less@3.9.0 less-loader@4.1.0 --save-dev</code></pre>



    </div>
     
    <div class="post-footer__meta"><p>更新于 2022-03-26</p></div> 
    <div class="post-entry__tags"><a href="/tags/webpack/" class="post-tags__link button"># webpack</a></div> 
</article>


    <div class="nav">
        <div class="nav__prev">
            
                <a href="/2022/04/06/%E5%90%8E%E5%8F%B0%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F1.0/" class="nav__link">
                    <div>
                        <svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="24" height="24"><path d="M589.088 790.624L310.464 512l278.624-278.624 45.248 45.248L400.96 512l233.376 233.376z" fill="#808080"></path></svg>
                    </div>
                    <div>
                        <div class="nav__label">
                            <!-- Previous Post -->
                            上一页
                        </div>
                        <div class="nav__title">
                            Vue2+ElementUI后台管理系统
                        </div>
                    </div>
                </a>
            
        </div>
        <div class="nav__next">
            
                <a href="/2022/03/17/vue%E7%9F%A5%E8%AF%86%E9%93%BA%E5%9E%AB/" class="nav__link">
                    <div>
                        <div class="nav__label">
                            <!-- Next Post -->
                            下一页
                        </div>
                        <div class="nav__title">
                            vue知识铺垫
                        </div>
                    </div>
                    <div>
                        <svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="24" height="24"><path d="M434.944 790.624l-45.248-45.248L623.04 512l-233.376-233.376 45.248-45.248L713.568 512z" fill="#808080"></path></svg>
                    </div>
                </a>
            
        </div>
    </div>



    <div class="post__comments content-card" id="comment">
        
    <h4>评论</h4>
    
    
    
    <div id="valine_container" class="valine_thread"></div>

    
    
    
    
    
    
    
    


    </div>



</main>

            <footer class="footer" style="font-size: 10px;">
     
    <a href="#" class="button" id="b2t" aria-label="回到顶部" title="回到顶部">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" width="32" height="32">
            <path d="M233.376 722.752L278.624 768 512 534.624 745.376 768l45.248-45.248L512 444.128zM192 352h640V288H192z" fill="currentColor"></path>
        </svg>
    </a>

    


    
    
    
        <span id="busuanzi_container_site_uv" hidden>
            <span>游客:</span>
            <span id="busuanzi_value_site_uv"></span>
            <span></span>
            
                <span> </span>
            
        </span>
    
    
        <span id="busuanzi_container_site_pv" hidden>
            <span>次数:</span>
            <span id="busuanzi_value_site_pv"></span>
            <span></span>
            
        </span>
    
 
 

 
    
    
    <p>
        <!-- Powered by <a href="https://hexo.io" target="_blank">Hexo</a> | Theme - <a href="https://github.com/ChrAlpha/hexo-theme-cards" target="_blank">Cards</a> -->
        <span style="color: black;">作者：张程</span>
    </p>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
        var a_idx = 0;
            jQuery(document).ready(function($) {
                $("body").click(function(e) {
                    var a = new Array("会当凌绝顶，一览众山小", "春蚕到死丝方尽，蜡炬成灰泪始干", "夕阳无限好，只是近黄昏",
                     "位卑未敢忘忧国", "纸上得来终觉浅，绝知此事要躬行", "人生自古谁无死，留取丹心照汗青", "山重水复疑无路，柳暗花明又一村" ,
                     "小荷才露尖尖角，早有蜻蜓立上头", "床前明月光，疑是地上霜", "春色满园关不住，一枝红杏出墙来", 
                     "山际见来烟，竹中窥落日", "道狭草木长，夕露沾我衣","天长地久有时尽，此恨绵绵无绝期");
                    var color = new Array("#7B7B7B", "#CE0000", "#FF0080","#E800E8","#921AFF","#2828FF",
                    "#0072E3","#00CACA","#02DF82","#00DB00","#8CEA00","#E1E100","#EAC100","#F75000"
                    ,"#B87070","#AFAF61","#6FB7B7","#9999CC","#B766AD");
                    var $i = $("<a href='#' style='text-decoration:none'/>").text(a[a_idx]);
                    a_idx = (a_idx + 1) % a.length;
                    var x = e.pageX,
                        y = e.pageY;
                    $i.css({
                        "z-index": 999999999999999999999999999999999999999999999999999999999999999999999,
                        "top": y - 20,
                        "left": x,
                        "position": "absolute",
                        "font-weight": "bold",
                        "color": color[Math.floor(Math.random()*color.length)]
                    });
                    $("body").append($i);
                    $i.animate({
                            "top": y - 180,
                            "opacity": 0
                        },
                        1500,
                        function() {
                            $i.remove();
                        });
                });
            });
        </script>
    <script>
        !function() {
            function o(w, v, i) {
                return w.getAttribute(v) || i
            }
            function j(i) {
                return document.getElementsByTagName(i)
            }
            function l() {
                var i = j("script"),
                w = i.length,
                v = i[w - 1];
                return {
                    l: w,
                    z: o(v, "zIndex", -1),
                    o: o(v, "opacity", 0.5),
                    c: o(v, "color", "0,0,0"),
                    n: o(v, "count", 99)
                }
            }
            function k() {
                r = u.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
                n = u.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
            }
            function b() {
                e.clearRect(0, 0, r, n);
                var w = [f].concat(t);
                var x, v, A, B, z, y;
                t.forEach(function(i) {
                    i.x += i.xa,
                    i.y += i.ya,
                    i.xa *= i.x > r || i.x < 0 ? -1 : 1,
                    i.ya *= i.y > n || i.y < 0 ? -1 : 1,
                    e.fillRect(i.x - 0.5, i.y - 0.5, 1, 1);
                    for (v = 0; v < w.length; v++) {
                        x = w[v];
                        if (i !== x && null !== x.x && null !== x.y) {
                            B = i.x - x.x,
                            z = i.y - x.y,
                            y = B * B + z * z;
                            y < x.max && (x === f && y >= x.max / 2 && (i.x -= 0.03 * B, i.y -= 0.03 * z), A = (x.max - y) / x.max, e.beginPath(), e.lineWidth = A / 2, e.strokeStyle = "rgba(" + s.c + "," + (A + 0.2) + ")", e.moveTo(i.x, i.y), e.lineTo(x.x, x.y), e.stroke())
                        }
                    }
                    w.splice(w.indexOf(i), 1)
                }),
                m(b)
            }
            var u = document.createElement("canvas"),
            s = l(),
            c = "c_n" + s.l,
            e = u.getContext("2d"),
            r,
            n,
            m = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
            function(i) {
                window.setTimeout(i, 1000 / 45)
            },
            a = Math.random,
            f = {
                x: null,
                y: null,
                max: 20000
            };
            u.id = c;
            u.style.cssText = "position:fixed;top:0;left:0;z-index:" + s.z + ";opacity:" + s.o;
            j("body")[0].appendChild(u);
            k(),
            window.onresize = k;
            window.onmousemove = function(i) {
                i = i || window.event,
                f.x = i.clientX,
                f.y = i.clientY
            },
            window.onmouseout = function() {
                f.x = null,
                f.y = null
            };
            for (var t = [], p = 0; s.n > p; p++) {
                var h = a() * r,
                g = a() * n,
                q = 2 * a() - 1,
                d = 2 * a() - 1;
                t.push({
                    x: h,
                    y: g,
                    xa: q,
                    ya: d,
                    max: 6000
                })
            }
            setTimeout(function() {
                b()
            }, 100)
    } ();
    </script>
</footer>

        </div>
        
    <script defer src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.1.0/dist/lazyload.min.js"></script>
    <script>
        window.lazyLoadOptions = {
            elements_selector: ".lazy",
            threshold: 0
        };
    </script>
 

 

 

 

 



 



 


    
 


    
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>

    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.4.1/dist/jquery.fancybox.min.css">

    
<script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.4.1/dist/jquery.fancybox.min.js"></script>

    <script>
        let lazyloadT = Boolean('true'),
            auto_fancybox = Boolean('false')
        if (auto_fancybox) {
            $(".post__content").find('img').each(function () {
                var element = document.createElement("a");
                $(element).attr("data-fancybox", "gallery");
                $(element).attr("href", $(this).attr("src"));
                if (lazyloadT) {
                    $(element).attr("href", $(this).attr("data-srcset"));
                }
                $(this).wrap(element);
            });
        } else {
            $(".post__content").find("fancybox").find('img').each(function () {
                var element = document.createElement("a");
                $(element).attr("data-fancybox", "gallery");
                $(element).attr("href", $(this).attr("src"));
                if (lazyloadT) {
                    $(element).attr("href", $(this).attr("data-srcset"));
                }
                $(this).wrap(element);
            });
        }
    </script>
 

 

 

 

 


    

    
    
    
    <script>
        function loadComment() {
            let e;
            (e = document.createElement("script")).src = 'https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js',
            document.body.appendChild(e);
            e.onload = () => {
                var valineConfig = {"appId":"c21JqSBfScFdueO44Scwp9Ba-gzGzoHsz","appKey":"apETP0QIekHzRXjTHvOnrQmS","placeholder":null,"path":null,"avatar":"retro","meta":["nick","mail","link"],"pageSize":null,"lang":null,"visitor":null,"highlight":null,"avatarForce":null,"recordIP":null,"serverURLs":null,"enableQQ":true,"requiredFields":["nick","mail"],"emojiCDN":null,"emojiMaps":null};
                valineConfig.el = '#valine_container';
                for (var i in valineConfig) {
                    if (valineConfig[i] === null) delete valineConfig[i];
                }
                new Valine(valineConfig);
            };
        }
    
        var runningOnBrowser = typeof window !== "undefined";
        var isBot = runningOnBrowser && !("onscroll" in window) || typeof navigator !== "undefined" && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent);
        var supportsIntersectionObserver = runningOnBrowser && "IntersectionObserver" in window;
    
        setTimeout(function () {
            if (!isBot && supportsIntersectionObserver) {
                var comment_observer = new IntersectionObserver(function(entries) {
                    if (entries[0].isIntersecting) {
                        loadComment();
                        comment_observer.disconnect();
                    }
                }, { threshold: [0] });
                comment_observer.observe(document.getElementById('comment'));
            } else {
                loadComment();
            }
        }, 1);
    </script>


    
    
    
    
    

    
    
    
    
    

    
    
    



    </body>
</html>
